package com.zkc;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

public class calculator extends JFrame {

    private JTextField textField;

    public calculator() {

        super("简易计算器");

        // 设置布局
        setLayout(new BorderLayout());

        // 文本框和Clear按钮
        JPanel topPanel = new JPanel(new BorderLayout());
        textField = new JTextField();
        textField.setHorizontalAlignment(JTextField.LEFT);//左对齐
        topPanel.add(textField, BorderLayout.CENTER);

        JButton clearButton = new JButton("C");
        clearButton.addActionListener(new ButtonClickListener());
        topPanel.add(clearButton, BorderLayout.EAST);

        add(topPanel, BorderLayout.NORTH);

        // 按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(4, 4, 5, 5));

        // 添加按钮
        String[] buttonLabels = {
                "7", "8", "9", "+",
                "4", "5", "6", "-",
                "1", "2", "3", "*",
                "0", ".", "=", "/"
        };

        for (String label : buttonLabels) {//将按钮循环添加进去
            JButton button = new JButton(label);
            button.addActionListener(new ButtonClickListener());
            buttonPanel.add(button);
        }

        add(buttonPanel, BorderLayout.CENTER);

        // 设置窗口属性
        setSize(430, 575);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }
    private void evaluateExpression() {//计算表达式
        String expression = textField.getText();
        try {
            ScriptEngineManager manager = new ScriptEngineManager();
            ScriptEngine engine = manager.getEngineByName("js");
            Object result = engine.eval(expression);
            textField.setText(result.toString());
        } catch (Exception ex) {
            textField.setText("Error");
        }
    }

    private void clear() {//清空
        textField.setText("");
    }

    private class ButtonClickListener implements ActionListener {//监听
        @Override
        public void actionPerformed(ActionEvent e) {
            JButton source = (JButton) e.getSource();
            String buttonText = source.getText();

            switch (buttonText) {
                case "=":
                    evaluateExpression();
                    break;
                case "C":
                    clear();
                    break;
                default:
                    textField.setText(textField.getText() + buttonText);
                    break;
            }
        }

    }

    private static void InitGlobalFont(Font font) {//设置字体
        FontUIResource fontRes = new FontUIResource(font);
        for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements();) {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if (value instanceof FontUIResource) {
                UIManager.put(key, fontRes);
            }
        }
    }

    public static void main(String[] args) {
        InitGlobalFont(new Font("alias", Font.PLAIN, 30));
        SwingUtilities.invokeLater(() -> new calculator());
    }
}